﻿using System;
using System.Collections.Generic;
using System.Text;
using X.DataFactory;
using X.Entity;
using System.Data;

namespace X.Data.Access
{
    public class News : INews
    {
        public bool Create(Entity.NewsInfo newsInfo)
        {
            string sSQL = @"INSERT INTO X_News(Title,Content,PostTime)
                            VALUES(@Title,@Content,@PostTime)";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@Title", DbType.String, newsInfo.Title);
            cmdParams.AddInParameter("@Content", DbType.String, newsInfo.Content);
            cmdParams.AddInParameter("@PostTime", DbType.Date, newsInfo.PostTime);

            return DBHelper.ExecuteSql(sSQL, cmdParams);
        }

        public bool Delete(int newsId)
        {
            string sSQL = @"DELETE FROM X_News WHERE Id = @Id)";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@Id", DbType.Int32, newsId);

            return DBHelper.ExecuteSql(sSQL, cmdParams);
        }

        public bool Update(Entity.NewsInfo newsInfo)
        {
            string sSQL = @"UPDATE X_News
                            SET Title = @Title,Content = @Content,PostTime = @PostTime
                            WHERE Id = @Id";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@Title", DbType.String, newsInfo.Title);
            cmdParams.AddInParameter("@Content", DbType.String, newsInfo.Content);
            cmdParams.AddInParameter("@PostTime", DbType.Date, newsInfo.PostTime);
            cmdParams.AddInParameter("@Id", DbType.Int32, newsInfo.Id);

            return DBHelper.ExecuteSql(sSQL, cmdParams);
        }

        public IList<NewsInfo> GetList(int topCount)
        {
            string sSQL = "SELECT TOP {0} * FROM X_News ORDER BY PostTime DESC";

            sSQL = string.Format(sSQL, topCount);

            IList<Entity.NewsInfo> list = new List<Entity.NewsInfo>();

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(sSQL))
            {
                while (reader.Read())
                {
                    list.Add(NewsFromReader(reader));
                }
            }

            return list;
        }

        public IList<Entity.NewsInfo> GetAllList()
        {
            string sSQL = "SELECT * FROM X_News ORDER BY PostTime DESC";

            IList<Entity.NewsInfo> list = new List<Entity.NewsInfo>();

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(sSQL))
            {
                while (reader.Read())
                {
                    list.Add(NewsFromReader(reader));
                }
            }

            return list;
        }

        public Entity.NewsInfo GetNewsInfo(int newId)
        {
            string sSQL = "SELECT * FROM X_News WHERE Id = @Id";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@Id", DbType.Int32, newId);

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(sSQL, cmdParams))
            {
                while (reader.Read())
                {
                    return NewsFromReader(reader);
                }
            }

            return new NewsInfo(true);
        }

        public NewsInfo GetNewsInfo(string title)
        {
            string sSQL = "SELECT * FROM X_News WHERE Title = @Title";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@Title", DbType.String, title);

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(sSQL, cmdParams))
            {
                while (reader.Read())
                {
                    return NewsFromReader(reader);
                }
            }

            return new NewsInfo(true);
        }

        NewsInfo NewsFromReader(NullableDataReader reader)
        {
            NewsInfo newsInfo = new NewsInfo();

            newsInfo.Id = reader.GetInt32("Id");
            newsInfo.Title = reader.GetString("Title");
            newsInfo.Content = reader.GetString("Content");
            newsInfo.PostTime = reader.GetDateTime("PostTime");

            return newsInfo;
        }
    }
}
